﻿using GPS.MODELS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GPS.DataAccessLayer;
using AutoMapper;
using PagedList;
using GPS.ENTITYS;
using GPS.COMMON;
using System.IO;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Oracle.ManagedDataAccess.Client;
using System.Data;
using Microsoft.AspNet.Identity;
using NPOI.HSSF.UserModel;
using OfficeOpenXml;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Threading.Tasks;

namespace GPS.Controllers.GPS_TheoDoiBienDong
{
    public class GPS_BienDongHocVienController : Controller
    {
        DbContextHelper<GPS_NVContext> _db = Singleton<DbContextHelper<GPS_NVContext>>.Inst;
        public GPS.MODELS.ApplicationUser getCurrentUser()
        {
            ApplicationDbContext dbUser = new ApplicationDbContext();
            string userId = User.Identity.GetUserId();
            ApplicationUser _currentUser = dbUser.Users.Find(userId);
            return _currentUser;
        }
        #region Index
        public ActionResult Index(ViewModelDHV SearchModel, int? currentPage)
        {
            initialCategorySearchAction(SearchModel);
            string hodem = null;
            string ten = null;
            if (SearchModel.pHV_TEN != null)
            {
                string str = SearchModel.pHV_TEN.Trim();
                int idx = str.LastIndexOf(' ');
                if (idx != -1)
                {
                    hodem = str.Substring(0, idx);
                    ten = str.Substring(idx + 1);
                }
                else
                {
                    hodem = SearchModel.pHV_TEN;
                    ten = SearchModel.pHV_TEN;
                }
            }

            var entities = _db.Filter<DHV>(o => (SearchModel.pHV_MA == null || o.HV_MA.Contains(SearchModel.pHV_MA))
                                                && (SearchModel.pHV_TEN == null
                                                   || o.HV_HODEM.ToUpper().Trim().Contains(hodem.ToUpper())
                                                    || o.HV_TEN.ToUpper().Trim().Contains(ten.ToUpper())
                                                    || (o.HV_HODEM.ToUpper().Trim().Contains(hodem.ToUpper().Trim())
                                                    && o.HV_TEN.ToUpper().Trim().Contains(ten.ToUpper().Trim())))
                                                && (SearchModel.pHV_DVGTVT_MA == null || o.HV_DVGTVT_MA == SearchModel.pHV_DVGTVT_MA)
                                                && (SearchModel.pHV_HANGDT_MA == null || o.HV_HANGDT_MA == SearchModel.pHV_HANGDT_MA)
                                                && (SearchModel.pHV_SOCMT == null || o.HV_SOCMT.Contains(SearchModel.pHV_SOCMT)
                                                && (SearchModel.pHV_KH_MA == null || o.HV_KH_MA.Contains(SearchModel.pHV_KH_MA))));
            ViewBag.TotalRow = entities == null ? "0" : entities.Count().ToString();
            Int16 icount = 1;
            var model = new List<DHV>();
            foreach (var currentEntity in entities)
            {
                currentEntity.STT = icount;
                model.Add(Mapper.Map<DHV>(currentEntity));
                icount++;
            }

            var pageIndex = SearchModel.Page ?? 1;
            SearchModel.SearchResults = model.ToPagedList(pageIndex, GPS.COMMON.Constants.PageSize);
            return View(SearchModel);
        }
        public void initialCategorySearchAction(ViewModelDHV viewModel)
        {
            ViewBag.HV_DVGTVT_MAs = new SelectList(_db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40"), "DVGTVT_MA", "DVGTVT_TEN");
            ViewBag.HV_HANGDT_MAs = new SelectList(_db.GetAll<SHANGDT>(), "HANGDT_MA", "HANGDT_TEN");

            string DVGTVT_MA = this.getCurrentUser().MA_TINHTP;
            ViewBag.checkuser = DVGTVT_MA;
            string getdvgtvt_Ma = GPS.DAL.Check_User.CheckUser(this.getCurrentUser().MA_TINHTP, viewModel.pHV_DVGTVT_MA);
            viewModel.pHV_DVGTVT_MA = getdvgtvt_Ma;
        }
        #endregion

        #region detail
        public ActionResult Detail(string id1, string id2)
        {
            string _ma = id1.ToString();
            string _madv = id2.ToString();
            DHV model = _db.GetOne<DHV>(o => o.HV_MA == id1 && o.HV_DVGTVT_MA == id2);

            CrudModelDHV _model = new CrudModelDHV();
            GPS.COMMON.Helpers.CopyObject<CrudModelDHV>(model, ref _model, new List<string> { "HV_ACTIVE" });
            _model.HV_ACTIVE = (model.HV_ACTIVE == "0" ? false : true);

            initialCategoryDetailAction(_model);
            return PartialView("_Detail", _model);
        }
        public void initialCategoryDetailAction(CrudModelDHV view)
        {
            ViewBag.HV_DVGTVT_MAs = _db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40" && o.DVGTVT_MA == view.HV_DVGTVT_MA).Select(x => x.DVGTVT_TEN).FirstOrDefault();
            ViewBag.HV_HANGDT_MAs = _db.Filter<SHANGDT>(x => x.HANGDT_MA == view.HV_HANGDT_MA).Select(x => x.HANGDT_TEN).FirstOrDefault();

            ViewBag.HV_NOICAPCMTs = _db.Filter<SDONVIHC>(o => o.DVHC_MA == "40" && o.DVHC_MA == view.HV_NOICT_DVHC_MA).Select(x => x.DVHC_TENDAYDU).FirstOrDefault();
            ViewBag.HV_QUOCTICH_MAs = _db.Filter<SQUOCTICH>(o => o.QTICH_MA == view.HV_QUOCTICH_MA).Select(x => x.QTICH_TENVN).FirstOrDefault();
        }
        #endregion

        #region"Biến động học viên"
        public ActionResult Edit(string id1, string id2)
        {
            string _HV_MA = id1;
            string _HV_DVGTVT_MA = id2;

            ViewBag.DVGTVT_MAs = new SelectList(_db.Filter<SDONVIGTVT>(o => o.DVGTVT_MAQL == "40"), "DVGTVT_MA", "DVGTVT_TEN");
            DHV model = _db.GetOne<DHV>(z=>z.HV_MA == _HV_MA && z.HV_DVGTVT_MA == _HV_DVGTVT_MA);
            CrudModelDLSHV _model = new CrudModelDLSHV();
            _model.LSHV_GV_MA = model.HV_MA;
            _model.LSHV_DVGTVT_MA = model.HV_DVGTVT_MA;
            ViewBag.HV_TEN = model.HV_HODEM + " " + model.HV_TEN;
            ViewBag.HV_HANGDT_MA = model.HV_HANGDT_MA;
            return PartialView("_Edit", _model);
        }

        [HttpPost]
        public ActionResult Edit(CrudModelDLSHV model, FormCollection form)
        {
            if (ModelState.IsValid)
            {

                DLSHV _model = new DLSHV();
                GPS.COMMON.Helpers.CopyObject<DLSHV>(model, ref _model);
                _model.LSHV_NGAYNHAP = DateTime.Now;
                _model.LSHV_NGUOINHAP = "admin";
                if (_model.LSHV_STATUS != "3")
                    _model.LSHV_LOP_MA_MOI = null;
                _model.ID = EFSequence.GetNextValue("SEQ_DLSPT_ID");
                _db.Insert<DLSHV>(_model);

                //update trạng thái DGV:
                string _id1 = model.LSHV_GV_MA;
                string _id2 = model.LSHV_DVGTVT_MA;
                var _dhv = _db.GetOne<DHV>(o => o.HV_MA == _id1 && o.HV_DVGTVT_MA == _id2);
                _dhv.HV_ACTIVE = model.LSHV_STATUS;
                _db.Update<DHV>(_dhv);

                string url = Url.Action("Index", "GPS_BienDongHocVien");
                return Json(new { success = true, url = url });
            }
            else
            {
                return PartialView("_Edit", model);
            }
        }
        #endregion
    }
}